package com.mytaobao.main;


import android.os.Bundle;
import android.support.annotation.Nullable;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.baseproject.BaseFragment;
import com.baseproject.BaseFragmentsActivity;
import com.baseproject.bottomnavigation.AHBottomNavigationHelper;
import com.baseproject.bottomnavigation.BottomNavigationModel;
import com.mytaobao.R;
import com.mytaobao.main.cart.CartFragment;
import com.mytaobao.main.firsttab.FirstTabFragment;
import com.mytaobao.main.weitao.WeiTaoFragment;

import java.util.LinkedHashMap;
import java.util.Map;


public class MainActivity extends BaseFragmentsActivity {

    FirstTabFragment mFirstTabFragment = new FirstTabFragment();
    WeiTaoFragment mWeiTaoFragment = new WeiTaoFragment();
    CartFragment mCartFragment = new CartFragment();

    AHBottomNavigationHelper mAHBottomNavigationHelper;

    Map<Integer, Integer> bottomNavigationMap = new LinkedHashMap<Integer, Integer>() {
        {
            put(R.string.bottom_navigation_item_home, R.drawable.navtab_home);
            put(R.string.bottom_navigation_item_we, R.drawable.navtab_we);
            put(R.string.bottom_navigation_item_help, R.drawable.navtab_help);
            put(R.string.bottom_navigation_item_cart, R.drawable.navtab_cart);
            put(R.string.bottom_navigation_item_user, R.drawable.navtab_user);
        }
    };

    BottomNavigationModel mBottomNavigationModel = new BottomNavigationModel(R.id.bottom_navigation,
            R.color.buttomSelectedColor, R.color.buttomUnSelectedColor, bottomNavigationMap);

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getFragmentContentId() {
        return R.id.fragment_container;
    }

    @Override
    protected void setDefaultFragment() {
        defaultFragment = mFirstTabFragment;
    }

    public FirstTabFragment getFirstTabFragment() {
        return (FirstTabFragment) defaultFragment;
    }

    AHBottomNavigation.OnTabSelectedListener onTabSelectedListener = new AHBottomNavigation.OnTabSelectedListener() {
        @Override
        public boolean onTabSelected(int position, boolean wasSelected) {
            if (!wasSelected) {
                switch (position) {
                    case 0:
                        addFragment(mFirstTabFragment);
                        break;
                    case 1:
                        addFragment(mWeiTaoFragment);
                        break;
                    case 3:
//                     if(getTopFragmentName() != "CartFragment") {
//                    Repository.getInstance(RemoteDataSource.getInstance(), LocalDataSource.getInstance(this))
//                            .getList("commodity",ItemCommodity.class, null, "")
//                            .subscribe(new Subscriber<List<ItemCommodity>>() {
//                                @Override
//                                public void onCompleted() {
//                                }
//
//                                @Override
//                                public void onError(Throwable e) {
//                                }
//
//                                @Override
//                                public void onNext(List<ItemCommodity> itemCommodities) {
//                                    addFragment(mCartFragment);
//                                }
//                            });
//                }
                        addFragment(mCartFragment);
                        break;
                }
            }
            return true;
        }
    };

    @Override
     protected void onBackStackChanged(BaseFragment baseFragment) {
        int navigationPosition = mAHBottomNavigationHelper.getBottomNavigation().getCurrentItem();
        int fragmentPosition = getPositionInBottomNavigation(baseFragment);
        if (fragmentPosition != -1 && navigationPosition != fragmentPosition) {
            mAHBottomNavigationHelper.getBottomNavigation().setCurrentItem(fragmentPosition);
        }
    }

    private int getPositionInBottomNavigation(BaseFragment baseFragment) {
        if (baseFragment instanceof FirstTabFragment) {
            return 0;
        } else if (baseFragment instanceof WeiTaoFragment) {
            return 1;
        } else if (baseFragment instanceof CartFragment) {
            return 3;
        }
        return -1;
    }

    @Override
    protected void _onCreate(@Nullable Bundle savedInstanceState) {
        mAHBottomNavigationHelper = new AHBottomNavigationHelper().initView(this, mBottomNavigationModel, onTabSelectedListener);
    }
}
